package com.comdo.newup_loan.module.limit;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.adapter.CarBrandAdapter;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.model.CarBrandBean;
import com.comdo.newup_loan.model.CarBrandItem;
import com.comdo.newup_loan.model.CarModelBean;
import com.comdo.newup_loan.model.CarModelItem;
import com.comdo.newup_loan.model.CarSeriesBean;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.CustomDrawerLayout;
import com.comdo.newup_loan.widget.LetterListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/9/27.
 */

public class CarBrandActivity extends BaseActivity<CarBrandPersenter> implements CarBrandContact.View, View.OnClickListener {

    @BindView(R.id.id_drawerlayout)
    CustomDrawerLayout mDrawerLayout;
    @BindView(R.id.id_drawerlayout1)
    CustomDrawerLayout mChildDrawerLayout;
    @BindView(R.id.id_drawer)
    LinearLayout mlinearLayout;
    @BindView(R.id.id_drawer1)
    LinearLayout mChildlinearLayout;
    @BindView(R.id.list_view)
    ListView mlistView;
    @BindView(R.id.list_view_ser)
    ListView mSerlistView;
    @BindView(R.id.list_view_mode)
    ListView mModellistView;
    @BindView(R.id.tv_ser_back)
    TextView mSerBackTextView;
    @BindView(R.id.tv_mode_back)
    TextView mMolBackTextView;
    private TextView overlay;
    private Handler handler;
    private Thread overlayThread;
    private List<String> mLetters = new ArrayList<String>();
    //右侧字母导航数据
    private List<String> letterItems = new ArrayList<String>();
    //品牌列表
    private List<CarBrandItem> carBrandItems = new ArrayList<CarBrandItem>();
    //车系列表
    private List<CarBrandItem> carSerItems = new ArrayList<CarBrandItem>();
    //车型列表
    private List<CarBrandItem> carmodelItems = new ArrayList<CarBrandItem>();
    private CarBrandAdapter myAdapter;
    private int CarModelId;
    private String CarModelString;
    public final static int RESULT_CODE=101;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.layout_car300;
    }

    @Override
    protected void init() {
        super.init();
        mPresenter.getList();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(CarBrandActivity.this, "选择品牌", false);
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerStateChanged(int arg0) {
            }

            @Override
            public void onDrawerSlide(View arg0, float arg1) {
            }

            @Override
            public void onDrawerOpened(View arg0) {
                mChildDrawerLayout.setVisibility(View.VISIBLE);
            }

            @Override
            public void onDrawerClosed(View arg0) {
                if (mChildDrawerLayout.isDrawerOpen(Gravity.END)) {
                    mChildDrawerLayout.closeDrawer(Gravity.END);
                }
                mChildDrawerLayout.setVisibility(View.GONE);
            }
        });
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mChildDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mSerBackTextView.setOnClickListener(this);
        mMolBackTextView.setOnClickListener(this);
        initOverlay();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    myAdapter.notifyDataSetChanged();
                }
            }
        };
        overlayThread = new Thread() {
            @Override
            public void run() {
                overlay.setVisibility(View.GONE);
            }
        };

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_ser_back:
                mDrawerLayout.closeDrawer(Gravity.END);
                break;
            case R.id.tv_mode_back:
                mChildDrawerLayout.closeDrawer(Gravity.END);
                break;
        }
    }

    @Override
    public CarBrandPersenter getmPresenter() {
        return new CarBrandPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
    }

    //获取品牌列表
    @Override
    public void getList(List<CarBrandBean> datas) {
        carBrandItems.clear();
        for (CarBrandBean item : datas) {
            letterItems.add(item.getInitial());
            CarBrandItem carBrandItem = new CarBrandItem();
            carBrandItem.setIsFont(1);
            carBrandItem.setName(item.getInitial());
            carBrandItems.add(carBrandItem);
            for (int i = 0; i <= item.getList().size(); i++) {
                mLetters.add(item.getInitial());
            }
            carBrandItems.addAll(item.getList());
        }
        myAdapter = new CarBrandAdapter(CarBrandActivity.this, carBrandItems, 0);
        mlistView.setAdapter(myAdapter);
        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (carBrandItems.get(position).getIsFont() == 0) {
                    mPresenter.getSeriesList(carBrandItems.get(position).getId());
                }
            }
        });
        initLetterListView();
    }

    //获取车系列表
    @Override
    public void getSeriesList(List<CarSeriesBean> datas) {
        carSerItems.clear();
        for (CarSeriesBean item : datas) {
            CarBrandItem carBrandItem = new CarBrandItem();
            carBrandItem.setIsFont(1);
            carBrandItem.setName(item.getFirstBrand());
            carSerItems.add(carBrandItem);
            carSerItems.addAll(item.getList());
        }
        mSerlistView.setAdapter(new CarBrandAdapter(CarBrandActivity.this, carSerItems, 0));
        mSerlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (carSerItems.get(position).getIsFont() == 0) {
                    mPresenter.getModelList(carSerItems.get(position).getId());
                    mChildDrawerLayout.openDrawer(Gravity.END);
                }
            }
        });
        mDrawerLayout.openDrawer(Gravity.END);
    }

    //获取车型列表
    @Override
    public void getModelList(List<CarModelBean> datas) {
        carmodelItems.clear();
        //这里接口分了三层。。。
        for (CarModelBean item : datas) {
            CarBrandItem carBrandItem = new CarBrandItem();
            carBrandItem.setIsFont(1);
            carBrandItem.setName(item.getYear());
            carmodelItems.add(carBrandItem);
            for (CarModelItem carModelItem : item.getTypeList()) {
                carmodelItems.addAll(carModelItem.getDisplacementList());
            }
        }
        mModellistView.setAdapter(new CarBrandAdapter(CarBrandActivity.this, carmodelItems, 1));
        mModellistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (carmodelItems.get(position).getIsFont() == 0) {
                    CarModelId = carmodelItems.get(position).getOtherSystemId();
                    CarModelString=carmodelItems.get(position).getName();
                    Intent intent = new Intent();
                    intent.putExtra("id", CarModelId);
                    intent.putExtra("name", CarModelString);
                    setResult(RESULT_CODE, intent);
                    mDrawerLayout.closeDrawer(Gravity.END);
                    finish();
                }
            }
        });
    }

    private void initLetterListView() {
        String[] b = letterItems.toArray(new String[letterItems.size()]);
        LetterListView.b = b;
        LetterListView mLetterListView = (LetterListView) findViewById(R.id.letter_list_view);
        mLetterListView.postInvalidate();
        mLetterListView
                .setOnTouchingLetterChangedListener(new LetterListView.OnTouchingLetterChangedListener() {

                    @Override
                    public void onTouchingLetterChanged(String s) {
                        int position = mLetters.indexOf(s);
                        mlistView.setSelection(position);
                        overlay.setText(s);
                        overlay.setVisibility(View.VISIBLE);
                        handler.removeCallbacks(overlayThread);
                        handler.postDelayed(overlayThread, 1500);

                    }
                });
    }

    private void initOverlay() {
        LayoutInflater inflater = LayoutInflater.from(this);
        overlay = (TextView) inflater.inflate(R.layout.letter_overlay, null);
        overlay.setVisibility(View.INVISIBLE);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                PixelFormat.TRANSLUCENT);
        WindowManager windowManager = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(overlay, lp);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getWindowManager().removeViewImmediate(overlay);
    }


}
